/*****************************************************************************************
 **  Author:dgz master
 **  Contact：jiduxiong@dagongzao.com
 **
 *****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import {getWarehouseByRole} from "../customsJs/WarehouseRole"
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: '',
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelRight: '',
    modelFooter: '',
  },
  tableAction: 'Wms_Stock', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    onInit() {
      //框架初始化配置前，
      //示例：在按钮的最前面添加一个按钮
      //   this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
      //     name: "按钮", //按钮名称
      //     icon: "el-icon-document", //按钮图标：https://element.eleme.cn/#/zh-CN/component/icon
      //     type: "primary", //按钮样式:https://element-plus.gitee.io/zh-CN/component/button.html
      //     //color:"#eee",//自定义按钮颜色
      //     onClick: function () {
      //       this.$message.success("点击了按钮");
      //     }
      //   });
      //示例：设置修改新建、编辑弹出框字段标签的长度
      // this.boxOptions.labelWidth = 150;
      this.queryFields = ['WarehouseId', 'StockCode']
    },

    //   let arr = [
    //     {
    //       key: 'WarehouseId',
    //       ClearArray: ['ZoneId', 'RackId'],
    //       url: 'api/Wms_Warehouse/getDetailPage',
    //       value: 'ZoneIdOption',
    //     },
    //   ]
    //      let ZoneIdOption = this.getOption('ZoneId')
    //      let RackIdOption = this.getOption('RackId')

    //   this.editFormOptions.flat().forEach((i) => {
    //     arr.forEach((i) => {
    //       if (i.field == i.key) {
    //         i.onChange = (v) => {
    //           this.editFormFields.ZoneId = null
    //           this.editFormFields.RackId = null
    //           ZoneIdOption.data = []
    //           RackIdOption.data = []
    //           for (let index = 0; index < i.ClearArray.length; index++) {
    //             let item = i.ClearArray[index]
    //             this.editFormFields[item] = null
    //             `${item}Option`.data = []
    //           }

    //           this.http
    //             .post(url, {
    //               rows: 9999,
    //               value: v,
    //             })
    //             .then((r) => {
    //               ZoneIdOption.data = r.rows
    //             })
    //         }
    //       }
    //     })

    //     if (i.field == 'WarehouseId') {
    //       i.onChange = (v) => {
    //         this.editFormFields.ZoneId = null
    //         this.editFormFields.RackId = null
    //         ZoneIdOption.data = []
    //         RackIdOption.data = []

    //         this.http
    //           .post('api/Wms_Warehouse/getDetailPage', {
    //             rows: 9999,
    //             value: v,
    //           })
    //           .then((r) => {
    //             ZoneIdOption.data = r.rows
    //           })
    //       }
    //     }
    //   })
    // },
    async onInited() {
      let ZoneIdOption = this.getOption('ZoneId')
      let RackIdOption = this.getOption('RackId')
      ZoneIdOption.dataKeys = {
        key: 'ZoneId',
        value: ['ZoneCode', 'ZoneName'],
      }
      RackIdOption.dataKeys = {
        key: 'RackId',
        value: ['RackCode', 'RackName'],
      }
      const WarehouseListByRole = await getWarehouseByRole()
      this.searchFormOptions.flat().forEach(i => {
        if(i.field == 'WarehouseId') {
          i.data = WarehouseListByRole

        }
      })
      this.editFormOptions.flat().forEach((i) => {
        if (i.field == 'WarehouseId') {
          i.data = WarehouseListByRole
          i.onChange = (v, content) => {
            if (content != 'model') {
              this.editFormFields.ZoneId = null
              this.editFormFields.RackId = null
            }
            ZoneIdOption.data = []
            RackIdOption.data = []

            this.http
              .post('api/Wms_Warehouse/getDetailPage', {
                rows: 9999,
                value: v,
              })
              .then((r) => {
                ZoneIdOption.data = r.rows
              })
          }
        }
        if (i.field == 'ZoneId') {
          i.onChange = (v, content) => {
            if (content != 'model') {
              this.editFormFields.RackId = null
            }
            RackIdOption.data = []
            this.http
              .post('api/Wms_Rack/getPageData', {
                rows: 9999,
                wheres: JSON.stringify([
                  {
                    name: 'ZoneId',
                    value: v,
                  },
                ]),
              })
              .then((r) => {
                RackIdOption.data = r.rows
              })
          }
        }
        if (i.field == 'StockRow' || i.field == 'StockLevel') {
          i.onKeyPress = (v) => {
            let rack = this.getOption('RackId')
            if (this.editFormFields.StockRow && this.editFormFields.StockLevel && this.editFormFields.RackId) {
              let tmp = rack.data.find((i) => i.RackId == this.editFormFields.RackId)
              if (!!tmp) {
                this.editFormFields.StockCode = `${tmp.RackCode}-${
                  this.editFormFields.StockRow < 10 ? '0' + this.editFormFields.StockRow : this.editFormFields.StockRow
                }-${
                  this.editFormFields.StockLevel < 10
                    ? '0' + this.editFormFields.StockLevel
                    : this.editFormFields.StockLevel
                }`
                this.editFormFields.StockName = `${tmp.RackCode}${
                  this.editFormFields.StockRow < 10 ? '0' + this.editFormFields.StockRow : this.editFormFields.StockRow
                }${
                  this.editFormFields.StockLevel < 10
                    ? '0' + this.editFormFields.StockLevel
                    : this.editFormFields.StockLevel
                }`
              }
            }
          }
        }
        if (i.field == 'AllowDiffMaterial') {
          i.onChange = (v) => {
            let AllowDiffLot = this.getOption('AllowDiffLot')
            if (v == 1) {
              AllowDiffLot.disabled = true
              this.editFormFields.AllowDiffLot = 1
            } else {
              AllowDiffLot.disabled = false
            }
          }
        }
        if (i.field == 'ForbidOutbound' || i.field == 'ForbidInbound') {
          i.onChange = (v) => {
            let ForbidInboundComments = this.getOption('ForbidInboundComments')
            let ForbidOutboundComments = this.getOption('ForbidOutboundComments')
            if (v == 1) {
              if (i.field == 'ForbidInbound') {
                ForbidInboundComments.required = true
              } else {
                ForbidOutboundComments.required = true
              }
            } else {
              if (i.field == 'ForbidInbound') {
                ForbidInboundComments.required = false
              } else {
                ForbidOutboundComments.required = false
              }
            }
          }
        }
      })
    },
    searchBefore(param) {
      return true
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id

      return true
    },
    delBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true
    },
    addAfter(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      //添加后刷新字典
      this.initDicKeys()
      return true
    },
    updateAfter(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      //添加后刷新字典
      this.initDicKeys()
      return true
    },
    delAfter(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      //添加后刷新字典
      this.initDicKeys()
      return true
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=="Add";
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段="xxx";
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      let ForbidInboundComments = this.getOption('ForbidInboundComments')
      let ForbidOutboundComments = this.getOption('ForbidOutboundComments')
      if (this.currentAction == 'update') {
        let WhIdOption = this.getOption('WarehouseId')
        let ZoneIdOption = this.getOption('ZoneId')
        WhIdOption.onChange(row.WarehouseId, 'model')
        ZoneIdOption.onChange(row.ZoneId, 'model')
        if (row.ForbidInbound == 1) {
          ForbidInboundComments.required = true
        }
        if (row.ForbidOutbound == 1) {
          ForbidOutboundComments.required = true
        }
      } else if (this.currentAction == 'Add') {
        let AllowDiffLot = this.getOption('AllowDiffLot')
        ForbidInboundComments.required = ForbidOutboundComments.required = false
        this.editFormFields.ForbidOutbound = this.editFormFields.ForbidInbound = 0
        AllowDiffLot.disabled = true

        this.editFormFields.IssueType = this.editFormFields.FreightStatus = '0'
      }
    },
  },
}
export default extension
